package com.taptech.doufu.ui.view.choice;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.constant.UmengEventName;
import com.taptech.doufu.bean.EventBean;
import com.taptech.doufu.bean.choice.BannerModel;
import com.taptech.doufu.util.GlideRoundTransform;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.ScreenUtil;

/**
 * Created by Administrator on 2017/4/13/013.
 */
public class ChoiceTopicImageView extends LinearLayout{
    private BannerModel bean;
    private String title;
    public ChoiceTopicImageView(Context context) {
        super(context);
    }

    public ChoiceTopicImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ChoiceTopicImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    private Context context;
    private View lay;
    private ImageView ivPicBg;

    public ChoiceTopicImageView(final Context context, final BannerModel model, final String title, final EventBean eventData){
        this(context);
        this.title = title;
        if (model != null) {
            View view = LayoutInflater.from(context).inflate(R.layout.view_booklist_item, null);
            this.context = context;
            lay = view.findViewById(R.id.lay);
            ivPicBg =  view.findViewById(R.id.ivPicBg);
            ivPicBg.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    ChoiceUtils.addEvent(getContext(),UmengEventName.JINGXUAN_CARD_CONTENT,eventData);
                    ChoiceUtils.clickBanner(context,bean,title);
                }
            });
            bean = model;
            changeDayModelBg();
            setImageHeight();
            updateData();
            int width = ScreenUtil.getScreenWidth(context);
            int height = (int) (width/4.0f);
            LayoutParams params = new LayoutParams(width, height);
            params.gravity = Gravity.CENTER;
            addView(view,params);
        }
    }




    private void hide() {
        lay.setVisibility(GONE);
    }

    private void setImageHeight() {
        int width =ScreenUtil.getScreenWidth(context);
        int height = (int) (width/4.0f);
        LayoutParams params = new LayoutParams(width, height);
        ivPicBg.setLayoutParams(params);
    }



    private void updateData() {
        if (bean != null &&
                !TextUtils.isEmpty(bean.getBanner_img())) {
            GlideUtil.displayImage(context,ivPicBg,bean.getBanner_img());
        } else{
            hide();
        }
    }



    public void changeDayModelBg(){
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            darkBg();
        }else {
            dayBg();
        }
    }


    private void dayBg() {
        ivPicBg.setAlpha(1.0f);
    }

    private void darkBg() {
        ivPicBg.setAlpha(Constant.imagViewAlpha);
    }
}
